<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<!-- errorPage="/errorPage.jsp" -->
<%@ page import="java.util.Vector,
                                   java.util.Date,
                                   java.util.Locale,
                                   com.epro.standard.frontend.util.FileInfoUtil,
                                   com.softplus.carrefour.hd.bltier.entity.CfhdCaseHead,
                                   com.softplus.carrefour.hd.bltier.entity.Country,
                                   java.text.SimpleDateFormat,
                                   com.softplus.carrefour.hd.bltier.entity.UserLevel,
                                   com.softplus.carrefour.hd.bltier.admin.IUserLevelViewer,
                                   com.softplus.carrefour.hd.bltier.search.ICaseSearchViewer,
                                   com.softplus.carrefour.hd.bltier.search.CCaseSearchManage,
                                   com.softplus.carrefour.hd.bltier.admin.UserLevelManage,
                                   com.softplus.carrefour.hd.bltier.admin.ICFHDSystemViewer,
                                   com.softplus.carrefour.hd.bltier.admin.CFHDSystemManage,
                                   com.softplus.carrefour.hd.bltier.admin.ICFHDModuleViewer,
                                   com.softplus.carrefour.hd.bltier.admin.CFHDModuleManage,
                                   com.softplus.carrefour.hd.bltier.admin.ICountryViewer,
                                   com.softplus.carrefour.hd.bltier.admin.CountryManage,
                                   com.softplus.carrefour.hd.CFHDSystem" %>
  <%@ include file="../include/eproFileInfo.jsp" %>
  <%@ include file="../include/spUserVOInclude.jsp"%>
  <%@ taglib uri="/WEB-INF/displaytag-12.tld" prefix="display"%>
  <%@ taglib uri="/WEB-INF/CalendarTag.tld" prefix="CalendarTag"%>
  <%
          Vector      systemVector;
          Vector      moduleVector;
          Vector      countryVector;
          
          SimpleDateFormat      dateInputFormat     = new SimpleDateFormat("d/MM/yyyy",Locale.US);
          SimpleDateFormat      dateDispFormat     = new SimpleDateFormat("d/M/yyyy",Locale.US);

          String    search_keyword   =   request.getParameter("keyword")!=null?request.getParameter("keyword"): "";
          String    search_countryID =   request.getParameter("countryID")!=null?request.getParameter("countryID"): "";
          String    search_system    =    request.getParameter("system")!=null?request.getParameter("system"): "";
          String    search_module    =    request.getParameter("module")!=null?request.getParameter("module"): "";
          String    search_status    =    request.getParameter("status")!=null?request.getParameter("status"): "";
          String    search_casetype  =    request.getParameter("casetype")!=null?request.getParameter("casetype"): "";
          String    startDate        =    request.getParameter("startDate")!=null?request.getParameter("startDate"): dateDispFormat.format(new Date());
          String    endDate          =    request.getParameter("endDate")!=null?request.getParameter("endDate"): dateDispFormat.format(new Date());
          String    submitted        =    request.getParameter("submit")!=null?request.getParameter("submit"): "";
          
          String    linkBack         =   request.getParameter("linkfrom")!=null?request.getParameter("linkfrom"):"/CFRL/CFRLWEBMN01.jsp";
          String    forBackURL       =    request.getParameter("forBackURL")!=null?request.getParameter("forBackURL"): linkBack;
         
//                                  System.out.println(" *** startDate: "+startDate+" \n endDate: "+endDate);
                                  System.out.println(" *** linkBack: "+linkBack);
        
          Date      search_from        = null;
          Date      search_to             = null;
          
          if(!startDate.equals("")){
                     search_from        = dateInputFormat.parse(startDate);
          }
          if(!endDate.equals("")){
                     search_to             = dateInputFormat.parse(endDate);
          }
          
          String          selectBoxWidth = "140px";
          String          scrollHieght   =  "170px";
          String          scrollFull            =  "330px";
          String          scrollNodata     =  "18px";
          //list all avaliable status
          String          statusColl[]       = {CfhdCaseHead.STATUS_VALIDATED, CfhdCaseHead.STATUS_SUBMITTED, CfhdCaseHead.STATUS_FINISH, 
                                                               CfhdCaseHead.STATUS_APPROVE,CfhdCaseHead.STATUS_CLOSE,CfhdCaseHead.STATUS_REJECTED, 
                                                               CfhdCaseHead.STATUS_CANCEL};
          String          caseTypeColl[]      = {CfhdCaseHead.CASE_USER_QUESTION, CfhdCaseHead.CASE_PROBLEM_REPORT, CfhdCaseHead.CASE_CHANGE_REQUEST, CfhdCaseHead.CASE_SERVICE_REQUEST};
          //CfhdCaseHead.getCaseTypeDesc(CfhdCaseHead.CASE_PROBLEM_REPORT)
          String	wholeModule    =    "";

  	ICFHDSystemViewer				systemManage		=	(CFHDSystemManage)CFHDSystem.getControlClass("SYSTEM");
		ICFHDModuleViewer				moduleManage	     =	(CFHDModuleManage)CFHDSystem.getControlClass("MODULE");
		ICountryViewer                            countryManage       =   (CountryManage) CFHDSystem.getControlClass("COUNTRY");
          ICaseSearchViewer                   searchManage       =   (CCaseSearchManage) CFHDSystem.getControlClass("CASE_SEARCH");
          FileInfoUtil			                  fileInfoUtil 		   =   (FileInfoUtil) session.getAttribute("fileInfoUtil")!=null?
                                                                                                         (FileInfoUtil) session.getAttribute("fileInfoUtil"):new FileInfoUtil();
          
             fileInfoUtil.setMenuIDCode("CFHDPCMT08 ");
             fileInfoUtil.setMenuName(" Search Cases ");
             fileInfoUtil.setMasterWording(" Search Cases ");
             fileInfoUtil.setMenuDesc(" Search for Cases ");
//             fileInfoUtil.setBackToMenu("/CFRL/CFRLWEBMN01.jsp");
             fileInfoUtil.setBackToMenu(forBackURL);

             session.setAttribute("fileInfoUtil",fileInfoUtil);
  %>
<%--               
          System.out.println(" * * * * * * * *  "+request.getRequestURL());
--%>
  <html>
  <head>
      <title>
        Search Cases  
    </title>
      <%@ include file="../include/spienocache.jsp" %>
     <link rel="stylesheet" href="../theme/defaultTheme.css" />
     <link rel="stylesheet" href="../theme/viewCaseTheme.css" />
     <script src="../script/spTitleRefresh.js" type="text/javascript"></script>
     <script src="../script/spToolbar.js" type="text/javascript"></script>
     <script src="../script/dateValidation.js" type="text/javascript"></script>
<style type="text/css">
<!--
     select{
          width: <%= selectBoxWidth %>;
          border: 1px  solid #0000;
     }
     
     input{
          border: 1px solid #2c6aa2;
          }
-->
</style>
<script type="text/javascript" language="Javascript">
<!-- 
    
          function calendarDialog(objID)
          {
               //obj of textfield
               var obj = eval('document.all.'+objID);
               //alert(obj.tagName);
               obj.value = window.showModalDialog('Calendar.jsp','window ','status:no;dialogHeight:231px;dialogWidth:262px;scroll:no');	
          }
           
          function checkdateBefore(dd1,dd2){
               var d1 = dd1.value;
               var d2 = dd2.value;
               var dtCh= "/"; //date separator
               var pos1=d1.indexOf(dtCh);
               var pos2=d1.indexOf(dtCh,pos1+1);
               var pos11=d2.indexOf(dtCh);
               var pos21=d2.indexOf(dtCh,pos11+1);
               //strip by date format
               var strDay=parseInt(d1.substring(0,pos1));
               var strMonth=parseInt(d1.substring(pos1+1,pos2));
               var strYear=parseInt(d1.substring(pos2+1))  ;
               
                var strDay2=parseInt(d2.substring(0,pos11));
               var strMonth2=parseInt(d2.substring(pos11+1,pos21));
               var strYear2=parseInt(d2.substring(pos21+1)) ;
              
               if(strYear2>=strYear){
//                    alert(strYear+' >='+strYear2)
                         if(strYear2==strYear){ //same year
//                                   alert(strMonth+' <='+strMonth2)
                                   if(strMonth<=strMonth2){
                                              if(strMonth==strMonth2){
                                              //alert(strDay+' <='+strDay2)
                                                       if(strDay<strDay2 || strDay==strDay2 ){
                                                       //alert(strDay+' < & ='+strDay2)
                                                            return true;
                                                       }
                                                       else{
                                                            alert('Please Check From/To Date Input!')
                                                            dd1.focus();
                                                            return false;
                                                       }
                                             }
                                        return true; //Month of from-date before to-date       
                                   }
                                    else{
                                        alert('Please Check From/To Date Input!')
                                        dd1.focus();
                                        return false;
                                   }
                         }
                   return true; //Year of from-date before to-date
               }
               alert('Please Check From/To Date Input!')
               dd1.focus();
               return false;
               
          }
          
          function checkSubmit(){
               var dt1=document.frm.startDate;
               var dt2=document.frm.endDate;
               var date1ok = false;
               var date2ok = false;
  //             /* 
               if(dt1.value!='' ){
                    if(validDate(dt1)){
                         date1ok = true;
                    }
               }else{
                    date1ok = true;   
               }
               
               if(dt2.value!='' ){
                    if(validDate(dt2)){
                         date2ok = true;                      
                    }
               }else{
                    date2ok = true;   
               }
               
               if(date1ok && date2ok){ 
                         if(dt1.value!='' && dt2.value!=''){
                                   if( checkdateBefore(dt1,dt2) ){
                                            // frm.submit();
                                             document.frm.gogo.click();
                                    }
                          }else{
                              document.frm.gogo.click();
                          }
               }   

          }
          
          function validDate(dt){
                if (isDate(dt.value)==false){
                    dt.focus();
                    return false;
               }
              return true;
          }
          
          function checkFormReset(){
			var	len		=	frm.module.length;

			// remove all from SELECT
			for (var k=len;k<=(len)&&(k>=0);k--)
			{
					frm.module.remove(k);
			}// end for remove data
               
                    var modulev=	document.createElement("OPTION");
                    modulev.text	=	'All';
                    modulev.value=	'';
                    frm.module.add(modulev);
          }
          
          function backToLastVisit(){
               document.location=frm.forBackURL.value;
          }
 -->
</script>
<%@ include file="../include/spAuthorization.jsp" %>
 <%--
     /*
     *    check user level if there're no level for him it'll kick back to mainmenu
     */
               String                           uLevel =null;
               
               try{
                           IUserLevelViewer      ulViwer  = (UserLevelManage)CFHDSystem.getControlClass("USER_LEVEL");
                                                                 uLevel    = ulViwer.getUserLevelByUserID(userVO.getUserId());
                    }catch(NullPointerException ne){
                         ne.printStackTrace();

//                         System.out.println("user not authorized");
//                         out.println("<script src=\"../script/Alert.js\" type=\"text/javascript\"></script>");
//                         out.println("<script type=\"text/javascript\">");
//                         out.println("if(showOInfoDialog(\"Authentication Failed!\", \"You might not authorized in Helpdesk System. Please Contact Administrator.\\n click OK  to continue.\", \"OK\" )){");
//                         out.println("  parent.location =parent.location; ");
//                         out.println(" } \n </script>");

                    }
                    
                    if(uLevel==null){
                        out.println("<script src=\"../script/Alert.js\" type=\"text/javascript\"></script>");
                        out.println("<script type=\"text/javascript\">");
                        out.println("if(showOInfoDialog(\"Authentication Failed!\", \"You might not be authorized in E-Helpdesk System. Please Contact Administrator.\\n click OK  to continue.\", \"OK\" )){");
                        out.println("  parent.location =parent.location; ");
                        out.println(" } \n </script>");                     
                    }
--%>
  </head>
<body style="margin-left:10px;">
<div id="processingLayer" style="visibility:hidden; position:absolute; left:100px; top:150px;">
	<img alt="Waiting..." src="../images/background/Processing.gif" width="585" height="136" border="0">
</div>
<%= fileInfoUtil.MasterHeadTable() %>
    <form name="frm" id="frm" action="<%= request.getRequestURI()%>" method="get">
    <input type="hidden" value="<%= forBackURL %>" name="forBackURL" >
      <table cellspacing="2" cellpadding="2" border="0" width="99%">
        <tbody>
        <tr>
        <td width="5%" nowrap="nowrap"> <!-- //fileInfoUtil.getBackToMenu() -->
               <a href="Javascript:backToLastVisit()">
              <img alt="Back To Menu" src="../images/icon/toolbar/nBackToMenu.gif" width="21" height="21" onmouseout="imgOut(this)" onmouseover="imgOver(this)"></a>
              <a href="<%= request.getRequestURI() %>"><img alt="Form Reset" src="../images/icon/toolbar/nClear.gif" onmouseout="imgOut(this)" onmouseover="imgOver(this)" onClick="frm.reset();"></a>
         </td>
            <td valign="top" align="center"><span class="titleFont">Keyword : </span>
              <input type="text" name="keyword" size="25" value="<%= search_keyword %>" />
              <input type="button"  value="  Search  " class="btn" onclick="checkSubmit()" />
              <input type="submit" name="submit" id="gogo" style="display:none;"/>
            </td>
          </tr>
          <tr>
            <td colspan="2">
              <fieldset style=" margin-left:20pt;" >
                <legend class="titleFont">
                  Filters
                </legend>
                <table cellspacing="2" cellpadding="2" border="0" width="99%">
                  <tbody>
                    <tr>
                      <th align="right">
                        Country : 
                      </th>
                      <td>
                              <select name="countryID">
                                   <option  value=""> All </option>
                              <%
                                        countryVector = countryManage.getAllCountry();
                                        for(int i=0; i<countryVector.size(); i++){
                                             Country   country = (Country) countryVector.elementAt(i);              
                              %>
                                        <option value="<%= country.getCountryId()%>" <%  if(search_countryID.equals(country.getCountryId())) out.print(" selected "); %>><%= country.getCountryName()%></option>
                              <%
                                        }
                              %>
                              </select>
                         </td>
                    </tr>
                    <tr>
                      <th align="right">&nbsp;Case&nbsp;Type :</th>
                      <td>
                        <select name="casetype">
                          <option value="">All</option>
                          <% 
                                        for(int i=0; i< caseTypeColl.length; i++){
                                        String    innerString = (String)caseTypeColl[i];
                              %>
                          <option value="<%= caseTypeColl[i] %>"  <%  if(search_casetype.equals(innerString)) out.print(" selected ");%> >
                            <%=  CfhdCaseHead.getStatusDesc(innerString) %>
                          </option>
                          <% 
                                   }
                              %>
                        </select>
                      </td>
                    </tr>
                    <tr>
                      <th align="right">
                        Status : 
                      </th>
                      <td>
                              <select name="status">
                               <option value=""> All </option>
                              <%
                                        for(int i=0; i< statusColl.length; i++){
                                        String    innerString = (String)statusColl[i];
                              %>
                                  <option value="<%= statusColl[i] %>" <% if(search_status.equals(innerString)) out.print(" selected ") ;%>> <%= CfhdCaseHead.getStatusDesc(innerString) %></option>
                              <%
                                   }
                              %>
                              </select>
                         </td>
                    </tr>
                    <tr>
                      <th align="right">
                        System : 
                      </th>
                      <td>
                         <%
                                   systemVector	=	systemManage.getListSystem();
                         %>
                              <select name="system" onchange="limitModule()">
                              <option value="">  All  </option>
                              <%
                                        for(int i=0; i<systemVector.size(); i++){
                              %>
                                   <option value="<%= systemVector.elementAt(i) %>" <% if(search_system.equals(systemVector.elementAt(i))) out.print(" selected ") ;%>><%= systemVector.elementAt(i) %></option>
                              <%   
                                        }
                              %>
                              </select>
                    </td>
                    </tr>
                    <tr>
                      <th  align="right">
                        Module : 
                      </th>
                      <td>
                                <select name="module">
                                   <option value="" <% if(search_module.equals("")) out.print(" selected "); %>>   All  </option>
                      <%
                                   moduleVector   =    moduleManage.getListModule();
                                   wholeModule	=	"'"+(String)moduleVector.elementAt(0)+"'";
                                   
							for (int i=0; i<moduleVector.size(); i++){
                                        String    moduleStr       = (String) moduleVector.elementAt(i);
                                        String    moduleName = "";
                                        
                                             if (i<(moduleVector.size()-1)){
                                                   wholeModule	=	wholeModule+",'"+moduleVector.elementAt(i+1)+"'";
                                             }
                                             
                                             if( !search_system.equals("") && moduleStr.indexOf(search_system)>-1){ 
                                                  moduleName =  moduleStr.substring(0,moduleStr.lastIndexOf("-")-1);
                                                 
                         System.out.println(search_system+" - module>> "+moduleStr);
                         
                      %>                               
                                   <option value="<%= moduleName %>" <% if(!search_module.equals("") &&  moduleName.indexOf(search_module)>-1) out.print(" selected "); %> ><%= moduleName %></option>
                    <%
                                             }
                                   }//end for systemVector
                                   
                    %>
                              </select>
                      </td>
                    </tr>
                    <tr>
                      <th  width="10%" align="right" nowrap="nowrap">
                        Open Date From :&nbsp;
                      </th>
                      <td nowrap="nowrap">
                      <%--
                          System.out.println("startDate: "+startDate+" \n endDate: "+endDate);
                      --%>
                      <input type="text" name="startDate" id="startDate" size="15" value="<%= startDate %>"
                             readonly="readonly" class="disabledTxtField"/>
                      <img src="../images/icon/calendar/nCalendar.gif"
                                alt="Calendar"
                                onclick="calendarDialog('startDate')"
                                style="cursor: hand"
                                onmouseover="this.src='../images/icon/calendar/oCalendar.gif'" 
                                onmouseout="this.src='../images/icon/calendar/nCalendar.gif'" />
                                <span class="titleFont">&nbsp;&nbsp;To :&nbsp;</span>
                         <input type="text" name="endDate" id="endDate" size="15" value="<%= endDate %>"
                                readonly="readonly" class="disabledTxtField"/>
                        <img src="../images/icon/calendar/nCalendar.gif"
                             alt="Calendar" onclick="calendarDialog('endDate')"
                             style="cursor: hand"
                             onmouseover="this.src='../images/icon/calendar/oCalendar.gif'"
                             onmouseout="this.src='../images/icon/calendar/nCalendar.gif'" />
                      </td>
                    </tr>
                  </tbody>
                </table>
              </fieldset>
            </td>
          </tr>
        </tbody>
      </table>
    </form>
    <%    
                    System.out.println(" request value : "+search_keyword+" \n case type: "+search_casetype+" \n country: "+search_countryID+" \n status: "+search_status+" \n system: "+search_system+" \n module: "+search_module+" \n from: "+search_from+" \n to: "+search_to);
                   //Fix for single quote searching...
                   if(search_keyword.indexOf("'")>-1)
                   try{
                    String kw = java.net.URLEncoder.encode(search_keyword);
                    String sgq = java.net.URLEncoder.encode("'");
                    String finalStr = com.epro.standard.frontend.util.StringUtil.replaceAll(kw,sgq,sgq+sgq,true);
                    //System.out.println(" ->"+java.net.URLDecoder.decode(finalStr)+"<-");
                    search_keyword = java.net.URLDecoder.decode(finalStr);
                   }catch(Exception e){
                    //e.printStacktrace();
                   }
                   String    divHeight = scrollHieght;
                  Vector    dataVec = searchManage.searchCaseBySystemAndModule(search_keyword, search_countryID, search_status, search_system, search_module,search_casetype, search_from, search_to);
     
//     System.out.println(" result vector size: "+dataVec.size());
     
                         session.setAttribute( "result", null );
                         session.setAttribute( "result", dataVec );
                         
              if(!submitted.equalsIgnoreCase("")){
   
    %>
    <table width="99%" border="0" cellpadding="0" cellspacing="0" >
    <tr><td class="titleFontleft">Search Result</td></tr>
    <tr><td>
    <div style="height:<%= divHeight %>;border:none;" class="tableContainer" id="tableContainer1">		
    <display:table name="sessionScope.result" id="result" defaultsort="1" styleClass="scrollTable" cellspacing="0"  cellpadding="0" 
                   decorator="com.softplus.carrefour.hd.frontend.displaytag.process.viewallcases.viewCaseTableWrapper">
      <display:column headerStyleClass=" fixedHeader" title="Case No." property="viewsearchresult" sortable="true" maxLength="30" align="left" style="width:130pt;" />
      <display:column headerStyleClass=" fixedHeader" title=" Subject " property="subject" align="left" sortable="true" maxLength="35" />
       <display:column headerStyleClass=" fixedHeader" title="Owner" property="searchcaseowner" sortable="true" align="left" maxLength="10" style="padding-left:5px;width:70pt;" />
       <display:column headerStyleClass=" fixedHeader" title="Severity" property="severity" align="left" sortable="true" style="padding-left:5px;width:50pt;" decorator="com.softplus.carrefour.hd.frontend.displaytag.process.viewallcases.serverityDecorator" />
       <display:column headerStyleClass=" fixedHeader" title="Status" property="caseStatus" align="left"  sortable="true" style="padding-left:5px;width:50pt;" decorator="com.softplus.carrefour.hd.frontend.displaytag.process.viewallcases.caseStatusDecorator"/>
    </display:table>
    </div>
    </td></tr>
    </table>
    <%    } %>
      <%= fileInfoUtil.MasterFootTable() %>
    <script type="text/javascript">
    <!--
     	function limitModule()
	{
			var sysNow	=	frm.system.value;
			var whole		=	new Array(<%=wholeModule%>);
			var	len		=	frm.module.length;

			// remove all from SELECT
			for (var k=len;k<=(len)&&(k>=0);k--)
			{
					frm.module.remove(k);
			}// end for remove data

               //add first item for all system selected.
					var moduleX	=	document.createElement("OPTION");
					moduleX.text	=	"All";
					moduleX.value	=	"";
					frm.module.add(moduleX);
                         
			if (sysNow!="")
			{
////					var module0	=	document.createElement("OPTION");
////					module0.text	=	"All";
////					frm.module.add(module0);
//			}else{
					// add data to SELECT
					for (var m=0;m<whole.length;m++)
					{
							if (whole[m].search(sysNow) != -1)
							{
									var	myRegExp	=	" - ";
									var	temp				=	whole[m].substring(0,whole[m].search(myRegExp));
									eval('var module'+m+'	=	document.createElement("OPTION")');
									eval('module'+m+'.text	=	temp');
									eval('module'+m+'.value=	temp');
									eval('frm.module.add(module'+m+')');
							}
					}// end for add data
                         frm.module.disabled = false;
			} // end if system not null
          }
    -->
    </script>
  </body>
</html>
